matrix output = J(2,5,.)

local c = 0
foreach outcome in completedCorps step1 step2 step3 step45  { 
local c=`c'+1


* Step 1: Convert to balanced panel of market level observations
clear
use ${data}teacher_data

collapse `outcome' da  (sum) n, by(CorpsYear region hs)


egen group = group(region hs)


* Step 2: Run synthetic control
tsset group CorpsYear
synth  `outcome'   `outcome'(2009) `outcome'(2010) `outcome'(2011) `outcome'(2012) `outcome'(2013) ///
 `marketsize' ,  trunit(2) trperiod(2014) 

*synth  `outcome'   `outcome'(2009) `outcome'(2010) `outcome'(2011)  `marketsize' ,  trunit(2) trperiod(2014) fig

matrix Ysynth = e(Y_synthetic)
matrix Ytreat = e(Y_treated)
matrix weight_mat = e(W_weights)

* Save weights
preserve
clear
svmat weight_mat
ren weight_mat1 row
ren weight_mat2 `outcome'
tempfile `outcome'
save ``outcome''

* Step 3: Save effect in output matrix and make a pretty figure

clear
svmat Ysynth
svmat Ytreat
gen CorpsYear = 2008+_n
gen effect = Ytreat-Ysynth

matrix output[1,`c'] = effect[6]

if "`outcome'"=="completedCorps" {
	tsset CorpsYear
	tsline Ysynth Ytreat, lcolor(black black) lpattern(dash solid) xtitle("Corps Year") ytitle("Completed Two-Year Committment") graphregion(color(white)) ylabel(,nogrid) ///
		legend(label(1 "Synthetic") label(2 "Treatment")) xline(2013.5, lcolor(black) lpattern(dot)) xlabel(2009(1)2014)
	graph export ${results}figure3_panelB.png, replace
}

 
* Step 4: Calculate relative post/pre RMSE
 restore 
forv m=1/8 {
 tempfile synth`m'
synth `outcome'  `outcome'(2009) `outcome'(2010) `outcome'(2011) `outcome'(2012) `outcome'(2013) ///
   `marketsize' `covariates'  ,  trunit(`m') trperiod(2014) keep(`synth`m'') replace
 matrix market‘m’ = e(RMSPE)
}

forv m=1/8 {
 
 use `synth`m'', clear


 keep _Y_treated _Y_synthetic _time
 drop if _time==.
 rename _time CorpsYear
 rename _Y_treated treat`m'
 rename _Y_synthetic counterfact`m'
 gen gap`m'=treat`m'-counterfact`m'
 sort CorpsYear
 save `synth`m'', replace
 
}

use `synth1'
forv m=2/8 {
 merge 1:1 CorpsYear using `synth`m''
 drop _merge
}
tempfile placebo
save `placebo', replace

forv m=1/8 {

 use `synth`m'', clear
 gen gap_sq = gap`m'*gap`m'
 egen postmean=mean(gap_sq) if CorpsYear==2014
 egen premean=mean(gap_sq) if CorpsYear<2014
 gen rmspe=sqrt(premean) if CorpsYear<2014
 replace rmspe=sqrt(postmean) if CorpsYear==2014
 gen ratio=rmspe/rmspe[_n-1] if CorpsYear==2014
 gen rmspe_post=sqrt(postmean) if CorpsYear==2014
 gen rmspe_pre=rmspe[_n-1] if CorpsYear==2014
 mkmat rmspe_pre rmspe_post ratio if CorpsYear==2014, matrix (market`m') 

}

* show post/pre-expansion RMSPE ratio for all states, generate histogram 
forv m=1/8 {
 matrix rownames market`m'= `m'
 matlist market`m', names(rows)
}
 mat market=market1\market2\market3\market4\market5\market6\market7\market8

clear
svmat market
gen market=_n
ren market1 rmspe_pre
ren market2 rmspe_post
ren market3 ratio
gsort -ratio
gen rank=_n
gen p=rank/_N

list rank p if market==2

sort market
matrix output[2,`c'] = p[2]


}
	



clear
svmat output
outsheet using ${results}table5_panelB_estimates.csv, comma names replace
	
	
clear
use `completedCorps'
foreach outcome in  step1 step2 step3 step45  { 
	merge 1:1 row using ``outcome'', nogen
}
outsheet using ${results}table5_panelB_weights.csv, comma names replace
	
	exit
